//************************************************************************
//***********************************************************************
//     main.c
//     - Copyright 2002 eixltd.com
//     - The main program
//.....- This version uses the CCS compiler for a PIC 16F877
//
//************************************************************************
//************************************************************************

#include <avr/io.h>
#include <avr/interrupt.h>

#include "def.h"

#include "nic_lib.h"
#include "mac_lib.h"
#include "iph_lib.h"
#include "udp_lib.h"
#include "tcp_lib.h"

//************************************************************************
//**    Variables used here:
u8      HA[6]={0x00,0x02,0x03,0x04,0x05,0x06};  // my MAC address  "192.168.0.13"
//169.254.43.100
//u8		varMyIP[4]={192,168,1,111};
u8		varMyIP[4]={169,254,43,100};
u16     main_tout;                           	// timeout counter
u1      main_itim;                          	// timeout semaphore
//************************************************************************


u8      myHA[6];           // GLOBAL: my MAC address (pre-initialised)
u32     myIP;              // GLOBAL: my IP  address (pre-initialised)

u16     udp_myPt;          // GLOBAL my port 
u16     udp_tuPt;          // GLOBAL 

u16     tcp_myPt;          // my TCP port 

u8 rune=0;


//************************************************************************

void initTimer0(void)
{
	// Clock select clk/1024 (From prescaler)
	TCCR0 |= (1<<CS02) | (1<<CS00);
	// Timer0 overflow interrupt enable
  	TIMSK |= 1<<TOIE0;
}


//************************************************************************
int main(void)
	{       
	u8 b;
	u16 cs,nb;
//****  Initialise PIC HW PORTS (PIC specific)...........................
	DDRA = 1;
	PORTA = 0xFF;
//........................................................................
//****  Initialise Timer. One second "tick" interrupt counter............
	initTimer0();
//........................................................................
//**** Initialise local Hardware Interfaces.............................
	myIP = 0;
	for (b=0; b<6; b++)
	{
		myHA[b]=HA[b];		
		if(b<4)
		{
			myIP |= (u32)varMyIP[b]<<((b<<1)<<2);
		}
	}
//	myIP=           0x642BFEA9;        // "169.254.43.100"
//	myIP= 0x6401A8C0;
	udp_myPt=       0xF401;             // my UDP port 500
	tcp_myPt=       0x5000;             // my TCP port 21 (http)
	nic_Init(0, HA);
	iph_Init();
	mac_Init();
	udp_Init();
	tcp_Init();
	main_itim=      FALSE;                 // timer interrupt vars
	main_tout=      0;
//........................................................................


//**** Do the Forever Loop..............................................
	while (1)
	{ //** Ethernet Controller handler...............................
		if (nic_Poll())       // poll controller for rx frame
		{ // Handle ARP, return 20 if frame was IP
			b=mac_Handler();
			if (b==20)
			{ // Handle IP, ret 40 if UDP, 50 if TCP, 32 if ping
				b=iph_Handler();
				//
				// handle UDP
				if (b==40)
				{ 
					udp_Handler(&cs,&nb);
					//if (nb.0)  //was
					if (nb==0)
					{
						udp_User(cs,nb);
					}
				}
				//
				/// Handle TCP
				if (b==50)
				{
					if(rune)
					{
						rune=1;
					}
					tcp_Handler();
				}
			}
			nic_RxEnd();
		}
		//............................................................
		//** Timeout operations.......................................
		/*if (main_itim)
		{ 
			tcp_Timer();        // here every sec or so
			main_itim=FALSE;
		}*/
		//............................................................
		//** Other user application round-robin operations go here...
		//
		//............................................................
	}
	return 1;
//........................................................................
}
//========================================================================
//========================================================================


//************************************************************************
//************************************************************************
//**    Timer interrupt
//      - Only used to generate a one second tick
//      - Every second a global flag (semaphore) is set
//      - This is tested during main program loop in order to do some op
//        thus avoid calling functions from the interrupt program itself 
//************************************************************************
ISR(TIMER0_OVF_vect)
{
	//**    One second timer.................................................
	if ((main_tout--)==0)
	{ 
		main_itim=TRUE;    // set this flag approx once per second
		main_tout=14;      // countdown to 1 sec 
		PORTA = 0;	
	}
	else
	{
		PORTA = 1;
	}
}
//========================================================================
//========================================================================
